<template>
	<!-- 补发/入库记录 -->
	<a-modal :title="dialogType == 'reissue' ? '补发' : '入库'" :width="1500" :visible="visible"
		:confirmLoading="confirmLoading" @ok="handleSubmit" @cancel="handleCancel">
		<a-spin :spinning="formLoading">
			<a-form :form="form">
				<a-form-item label="送货单号" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback="">
					<a-input placeholder="请输入"
						v-decorator="['defectiveSendOrderNumber', { rules: [{ required: true, message: '请输入！' }] }]" />
				</a-form-item>
			</a-form>
			<a-table ref="table" style="margin-top: 15px;" size="default" :scroll="{ x: 1000, y: 1000 }"
				:columns="dialogType == 'reissue' ? columns : columns1" :data-source="dataSource">
				<span slot="reissueNum" slot-scope="text,record">
					<a-input-number :min="1" v-model="record.bfNum" :max="record.num" />
				</span>
				<span slot="storeNum" slot-scope="text,record">
					<a-input-number :min="1" v-model="record.storeNum"
						:max="record.reissueNum - (record.rkNum ? record.rkNum : 0)" />
				</span>
			</a-table>
		</a-spin>
	</a-modal>
</template>

<script>
import {
	detail,
	getMaterialByDefectivePoolId,
	reissue,//补发
	confirmWarehousing,//入库
} from '@/api/modular/main/badPool'
import { STable } from '@/components'
import { getUrlParam } from '@/utils/util'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import Vue from 'vue'

export default {
	components: {
		STable,
	},
	props: ['materialType'],
	data() {
		return {
			dialogType: '',
			labelCol: {
				xs: { span: 2 },
				sm: { span: 2 }
			},
			wrapperCol: {
				xs: { span: 8 },
				sm: { span: 8 }
			},
			orgTree: [],
			visible: false,
			formLoading: false,
			confirmLoading: false,
			route_path: '',
			form: this.$form.createForm(this),
			columns: [
				{
					title: '序号',
					width: '80px',
					customRender: (text, record, index) => `${index + 1}`
				},
				{
					title: '料号',
					dataIndex: 'emsMaterialResult.materialCode',
					width: '220px',
				},
				{
					title: '名称',
					dataIndex: 'emsMaterialResult.materialName',
					width: '270px',
				},
				{
					title: '规格',
					width: '220px',
					dataIndex: 'emsMaterialResult.materialModelNumber'
				},
				{
					title: '采购价',
					width: '120px',
					dataIndex: 'emsMaterialResult.purchasePrice'
				},
				{
					title: '生产单号',
					dataIndex: 'orderNumber',
					width: '120px',
					customRender: (text, record, index) => record.orderNumber ? record.orderNumber : '-'
				},
				{
					title: '不良数量',
					dataIndex: 'num',
					width: '120px',
					align: 'center',
				},
				{
					title: '待补发数量',
					dataIndex: '',
					width: '120px',
					align: 'center',
					customRender: (text, record, index) => record.num - (record.reissueNum ? record.reissueNum : 0)
				},
				{
					title: '已补发数量',
					dataIndex: 'reissueNum',
					width: '120px',
					align: 'center',
				},
				{
					title: '本次补发数量',
					align: 'center',
					width: '150px',
					fixed: 'right',
					scopedSlots: { customRender: 'reissueNum' }
				}
			],
			columns1: [
				{
					title: '序号',
					width: '80px',
					customRender: (text, record, index) => `${index + 1}`
				},
				{
					title: '料号',
					dataIndex: 'emsMaterialResult.materialCode',
					width: '220px',
				},
				{
					title: '名称',
					dataIndex: 'emsMaterialResult.materialName',
					width: '270px',
				},
				{
					title: '规格',
					width: '220px',
					dataIndex: 'emsMaterialResult.materialModelNumber'
				},
				{
					title: '采购价',
					width: '120px',
					dataIndex: 'emsMaterialResult.purchasePrice'
				},
				{
					title: '采购单号',
					dataIndex: 'orderNumber',
					width: '120px',
					customRender: (text, record, index) => record.orderNumber ? record.orderNumber : '-'
				},
				{
					title: '不良数量',
					dataIndex: 'num',
					width: '120px',
					align: 'center',
				},
				{
					title: '待入库数量',
					dataIndex: '',
					width: '120px',
					align: 'center',
					customRender: (text, record, index) => record.reissueNum - (record.rkNum ? record.rkNum : 0)
				},
				{
					title: '已入库数量',
					dataIndex: 'rkNum',
					width: '120px',
					align: 'center',
				},
				{
					title: '本次入库数量',
					align: 'center',
					width: '150px',
					fixed: 'right',
					scopedSlots: { customRender: 'storeNum' }
				}
			],
			dataSource: [],
		}
	},
	created() {
		this.route_path = getUrlParam(this.$route.path).materialType
		console.log("route_path", this.route_path);
	},
	methods: {
		// 初始化方法
		init(dialogType, record) {
			console.log(dialogType, record);
			this.dialogType = dialogType
			this.visible = true;
			if (!this.managerOrFinance()) {
				this.columns.splice(4, 1);
				this.columns1.splice(4, 1);
			}
			if (this.dialogType == "reissue") {//补发
				this.dataSource = JSON.parse(JSON.stringify(record)).map(val => {
					return {
						...val,
						bfNum: val.num - (val.reissueNum ? val.reissueNum : 0)
					}
				})
			} else {
				this.dataSource = JSON.parse(JSON.stringify(record)).map(val => {
					return {
						...val,
						storeNum: val.reissueNum - (val.rkNum ? val.rkNum : 0)
					}
				})
			}

		},
		handleSubmit() {
			const { form: { validateFields } } = this
			this.confirmLoading = true
			validateFields((errors, values) => {
				if (!errors) {
					let warehousingList = this.dataSource.map(val => {
						return {
							defectivePoolMaterialId: val.id,
							num: val.storeNum,
						}
					})
					let params = {
						id: this.dataSource[0].defectivePoolId,
						defectiveSendOrderNumber: values.defectiveSendOrderNumber,
						warehousingList: warehousingList
					}
					confirmWarehousing(params).then(res => {
						this.$message.success("操作成功");
						this.handleCancel();
						this.$emit("ok");
						this.confirmLoading = false
					}).catch(err => {
						this.confirmLoading = false;
					})
				} else {
					this.confirmLoading = false
				}
			})
			// this.confirmLoading = true
			// if (this.dialogType == 'reissue') {
			// 	let reissueMaterialList = this.dataSource.map(val => {
			// 		return {
			// 			defectivePoolMaterialId: val.id,
			// 			num: val.bfNum,
			// 		}
			// 	})
			// 	let params = {
			// 		id: this.dataSource[0].defectivePoolId,
			// 		reissueMaterialList: reissueMaterialList
			// 	}
			// 	console.log(params);
			// 	reissue(params).then(res => {
			// 		this.$message.success("操作成功");
			// 		this.handleCancel();
			// 		this.$emit("ok");
			// 		this.confirmLoading = false
			// 	}).catch(err => {
			// 		this.confirmLoading = false;
			// 	})
			// } else {
			// 	let warehousingList = this.dataSource.map(val => {
			// 		return {
			// 			defectivePoolMaterialId: val.id,
			// 			num: val.storeNum,
			// 		}
			// 	})
			// 	let params = {
			// 		id: this.dataSource[0].defectivePoolId,
			// 		warehousingList: warehousingList
			// 	}
			// 	confirmWarehousing(params).then(res => {
			// 		this.$message.success("操作成功");
			// 		this.handleCancel();
			// 		this.$emit("ok");
			// 		this.confirmLoading = false
			// 	}).catch(err => {
			// 		this.confirmLoading = false;
			// 	})
			// }
		},
		handleCancel() {
			this.visible = false;
			this.form.resetFields();
		},
	}
}
</script>

<style>
.inputDiv {
	width: 100%;
	line-height: 32px;
	cursor: pointer;
	background: #eee;
	border: 1px solid #bbb;
	padding-left: 10px;
}
</style>
